//
//  NewTaskHeader.m
//  rrqs
//
//  Created by mac book on 2019/5/5.
//

#import "NewTaskHeader.h"


@interface NewTaskHeader ()
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UILabel* labelShipFee;
@property(nonatomic,strong) UILabel* labelCreateTime;
@property(nonatomic,strong) UIImageView* line;
@property(nonatomic,strong) UILabel* labelDaoTime;

@end
@implementation NewTaskHeader

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
    }
    return self;
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self addSubview:self.topView];
    [self addSubview:self.labelShipFee];
    [self addSubview:self.labelCreateTime];
    [self addSubview:self.line];
    [self addSubview:self.labelDaoTime];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(20);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    [self.labelShipFee mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.bottom.equalTo(self.line.mas_top);
        make.left.equalTo(self);
        make.width.equalTo(self).multipliedBy(0.33);
    }];
    
    [self.labelCreateTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.equalTo(self.labelShipFee);
        make.left.equalTo(self.labelShipFee.mas_right);
    }];
    
    [self.labelDaoTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.labelCreateTime);
        make.left.equalTo(self.labelCreateTime.mas_right);
        make.right.equalTo(self);
        
    }];
    
}


#pragma mark =====================================================  Public method
-(void)setEntity:(MOrder *)entity{
    if(entity){
        _entity = entity;
        NSString* time =  @"";
        NSString* mark =  @"";
        if(entity.status == expressStatusWating){
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"接单时间";
        }else if(entity.status == expressStatusIng){
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"接单时间";
        }else{
            time = [WMHelper timeStampConvertToDateString:entity.addTime formate:@"HH:mm"];
            mark =  @"下单时间";
        }
        NSString* money = [NSString stringWithFormat:@"￥%@",entity.shipFee];
        NSString*strMoney = [NSString stringWithFormat:@"%@ \n 本单预计收入",money];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        
        
        
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
    }
}
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList
{
    if(waiMaiDodelHList){
        _waiMaiDodelHList = waiMaiDodelHList;
        NSString* time =  @"";
        NSString* mark =  @"";
        time = [WMHelper timeStampConvertToDateString:waiMaiDodelHList.second formate:@"HH:mm"];
        mark =  @"预计送达时间";
        
        NSString* money = [NSString stringWithFormat:@"￥%@",waiMaiDodelHList.shipping_fee];
        NSString* shouyi = [NSString stringWithFormat:@"+%@(奖励)",waiMaiDodelHList.extra_reward];
        NSString*strMoney = [NSString stringWithFormat:@"%@%@ \n 本单预计收入",money,shouyi];
        
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.f],NSForegroundColorAttributeName:[UIColor redColor]} range:[strMoney rangeOfString:shouyi]];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.f],NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:[strMoney rangeOfString:@"(奖励)"]];
        [attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(0.35 * (24 - 10)) range:[strMoney rangeOfString:shouyi]];
        
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        
        
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
        
        NSLog(@"跑腿=====%@",waiMaiDodelHList.daojishi);
        if (waiMaiDodelHList.daojishi.length == 0) {
            
            self.labelDaoTime.text = @"已超时";
            self.labelDaoTime.textColor = [UIColor redColor];
            waiMaiDodelHList.isTimeOut = YES;
            [[NSNotificationCenter defaultCenter]removeObserver:self];

        }else{
            
            
            if (waiMaiDodelHList.timeOut) {
                
                self.labelDaoTime.text = @"已超时";
                self.labelDaoTime.textColor = [UIColor redColor];
                waiMaiDodelHList.isTimeOut = YES;
                [[NSNotificationCenter defaultCenter]removeObserver:self];

            }else{
                
                int seconds = [waiMaiDodelHList.daojishi intValue];
                if (seconds<= 0) {
                    self.labelDaoTime.text = @"已超时";
                    self.labelDaoTime.textColor = [UIColor redColor];
                    waiMaiDodelHList.isTimeOut = YES;
                    [[NSNotificationCenter defaultCenter]removeObserver:self];

                }else{
                    
                    waiMaiDodelHList.timeCount = seconds;
                    waiMaiDodelHList.isTimeOut = NO;
                    
                    
                    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
                    
                }
                
            }

            
            
        }
        
    }
}
- (void)setWaiMaiDodelList:(WJWaiMaiDodelList *)waiMaiDodelList
{
    if(waiMaiDodelList){
        _waiMaiDodelList = waiMaiDodelList;
        NSString* time =  @"";
        NSString* mark =  @"";
        time = [WMHelper timeStampConvertToDateString:waiMaiDodelList.second formate:@"HH:mm"];
        mark =  @"预计送达时间";
        
        NSString* money = [NSString stringWithFormat:@"￥%@",waiMaiDodelList.returnpay];
        NSString* shouyi = [NSString stringWithFormat:@"+%@(奖励)",waiMaiDodelList.extra_reward];
        NSString*strMoney = [NSString stringWithFormat:@"%@ %@ \n 本单预计收入",money,shouyi];
        NSMutableAttributedString* attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:money]];
        
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.f],NSForegroundColorAttributeName:[UIColor redColor]} range:[strMoney rangeOfString:shouyi]];
        
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10.f],NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:[strMoney rangeOfString:@"(奖励)"]];
        [attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(0.35 * (24 - 10)) range:[strMoney rangeOfString:shouyi]];
        
        
        self.labelShipFee.attributedText = attributeStr;
        self.labelShipFee.textAlignment = NSTextAlignmentCenter;
        
        
        NSString* strTime = [NSString stringWithFormat:@"%@ \n %@",time, mark];
        attributeStr  = [[NSMutableAttributedString alloc]initWithString:strTime];
        [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strTime rangeOfString:time]];
        self.labelCreateTime.attributedText = attributeStr;
        self.labelCreateTime.textAlignment = NSTextAlignmentCenter;
        
        NSLog(@"外卖=====%@",waiMaiDodelList.daojishi);

        if (waiMaiDodelList.daojishi.length == 0) {
            
            self.labelDaoTime.text = @"已超时";
            self.labelDaoTime.textColor = [UIColor redColor];
            waiMaiDodelList.isTimeOut = YES;
            [[NSNotificationCenter defaultCenter]removeObserver:self];

        }else{
            
            if (waiMaiDodelList.timeOut) {

                self.labelDaoTime.text = @"已超时";
                self.labelDaoTime.textColor = [UIColor redColor];
                waiMaiDodelList.isTimeOut = YES;
                [[NSNotificationCenter defaultCenter]removeObserver:self];

            }else{
                
                int seconds = [waiMaiDodelList.daojishi intValue];
                if (seconds<= 0) {
                    self.labelDaoTime.text = @"已超时";
                    self.labelDaoTime.textColor = [UIColor redColor];
                    waiMaiDodelList.isTimeOut = YES;
                    [[NSNotificationCenter defaultCenter]removeObserver:self];

                }else{
                    
                    waiMaiDodelList.timeCount = seconds;
                    waiMaiDodelList.isTimeOut = NO;
                    
                    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownListNotification) name:OYCountDownNotification object:nil];
                    
                }
                
            }
            
            
            
        }
        
        
        
    }
}

#pragma mark =====================================================  property package
-(UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [UIColor colorWithRed:242/255.f green:242/255.f blue:242/255.f alpha:1.0];
    }
    return _topView;
}
-(UILabel *)labelShipFee{
    if(!_labelShipFee){
        _labelShipFee = [[UILabel alloc]init];
        _labelShipFee.numberOfLines = 0;
        _labelShipFee.lineBreakMode = NSLineBreakByCharWrapping;
        _labelShipFee.font = [UIFont systemFontOfSize:14.f];
        _labelShipFee.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelShipFee;
}

-(UILabel *)labelCreateTime{
    if(!_labelCreateTime){
        _labelCreateTime = [[UILabel alloc]init];
        _labelCreateTime.numberOfLines = 0;
        _labelCreateTime.lineBreakMode = NSLineBreakByCharWrapping;
        _labelCreateTime.font = [UIFont systemFontOfSize:14.f];
        _labelCreateTime.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
    }
    return _labelCreateTime;
}

-(UILabel *)labelDaoTime{
    if(!_labelDaoTime){
        _labelDaoTime = [[UILabel alloc]init];
        _labelDaoTime.numberOfLines = 0;
        _labelDaoTime.lineBreakMode = NSLineBreakByCharWrapping;
        _labelDaoTime.font = [UIFont systemFontOfSize:14.f];
        _labelDaoTime.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
        _labelDaoTime.textAlignment = NSTextAlignmentCenter;
        
    }
    return _labelDaoTime;
}


-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor =  [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _line;
}


#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    int timeInterval;
    if (self.waiMaiDodelHList.countDownSource) {
        timeInterval = [kCountDownManager timeIntervalWithIdentifier:self.waiMaiDodelHList.countDownSource];
    }else {
        timeInterval = kCountDownManager.timeInterval;
    }
    
    
    int countDown = 0;
    if (self.waiMaiDodelHList.isTimeOut) {
        
        countDown =  self.waiMaiDodelHList.timeCount + timeInterval;
    }else{
        
        countDown =  self.waiMaiDodelHList.timeCount - timeInterval;
        
    }
    
    
    if (self.waiMaiDodelHList.isTimeOut) {
        self.labelDaoTime.text = @"已超时";
        self.labelDaoTime.textColor = [UIColor redColor];
        
    }else{
        self.labelDaoTime.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
        
//        NSString *restTime = [NSString stringWithFormat:@"%02d:%02d:%02d",(countDown/3600),(countDown/60)%60, countDown%60];
    
        NSString *restTime = [NSString stringWithFormat:@"%02d:%02d",(countDown/60), countDown%60];

        NSString* resTime = [NSString stringWithFormat:@"%@ \n %@",restTime, @"剩余"];
        NSMutableAttributedString *attribute  = [[NSMutableAttributedString alloc]initWithString:resTime];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[resTime rangeOfString:restTime]];
        self.labelDaoTime.attributedText = attribute;
        
        
        if (countDown <= 0) {/// 当倒计时到了进行回调
            // 回调给控制器
            if (self.countDownZero) {
                self.countDownZero(self.waiMaiDodelHList);
            }
            return;

        }
    }
    
  
}

- (void)countDownListNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    
    int timeInterval;
    if (self.waiMaiDodelList.countDownSource) {
        timeInterval = [kCountDownManager timeIntervalWithIdentifier:self.waiMaiDodelList.countDownSource];
    }else {
        timeInterval = kCountDownManager.timeInterval;
    }
    
    
    int countDown = 0;
    if (self.waiMaiDodelList.isTimeOut) {
        
        countDown =  self.waiMaiDodelList.timeCount + timeInterval;
    }else{
        countDown =  self.waiMaiDodelList.timeCount - timeInterval;
    }
    
    if (self.waiMaiDodelList.isTimeOut) {
        self.labelDaoTime.text = @"已超时";
        self.labelDaoTime.textColor = [UIColor redColor];
        
    }else{
        self.labelDaoTime.textColor = [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
        
        NSString *restTime = [NSString stringWithFormat:@"%02d:%02d",(countDown/60), countDown%60];

        NSString* resTime = [NSString stringWithFormat:@"%@ \n %@",restTime, @"剩余"];
        NSMutableAttributedString *attribute  = [[NSMutableAttributedString alloc]initWithString:resTime];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[resTime rangeOfString:restTime]];
        self.labelDaoTime.attributedText = attribute;
        
        
        if (countDown <= 0) {/// 当倒计时到了进行回调
            // 回调给控制器
            // 回调给控制器
            if (self.countDownListZero) {
                self.countDownListZero(self.waiMaiDodelList);
            }
            return;

        }
    }
    

    
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}



@end
